import { createRouter, createWebHashHistory } from "vue-router";
import store from "../store";
const routes = [
  {
    path: "/",
    name: "Main",
    component: () => import("../views/Main.vue"),
    redirect: "/home",
    children: [
      // {
      //   path: "/home",
      //   name: "home",
      //   component: () => import("../views/Home/Home.vue"),
      // },
      // {
      //   path: "/user",
      //   name: "user",
      //   component: () => import("../views/User/User.vue"),
      // },
      // {
      //   path: "/pageOne",
      //   name: "pageOne",
      //   component: () => import("../views/Other/pageOne.vue"),
      // },
      // {
      //   path: "/pageTwo",
      //   name: "pageTwo",
      //   component: () => import("../views/Other/pageTwo.vue"),
      // },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login/Login.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  store.commit("getToken");
  const token = store.state.token;
  if (!token && to.name != "login") {
    router.push({ name: "login" });
  } else if (token && to.name == "login") {
    router.push({ name: "login" });
  } else {
    next();
  }
});

export default router;
